/**
 * 新闻相关功能JS文件
 * 
 * @author		布衣才子
 * @date		2012-09-29
 * @email		work.jerryliu@gmail.com
 * @qq			394969553
 * @version		v1.0
 * @copyright	copyright 2012-2014	YeeYi.com All Rights Reserved	
 */

/**
 * API Host
 */
var apiHost = "http://www.yeeyi.com/bbs/portal.php?jsoncallback=?&mod=mobile";

/**
 * 分页参数
 */
var newsPage = 1;
var b64 = new Base64();
var j = 1;

/**
 * 网络访问函数
 * @param {Object} url	请求的网络地址
 * @param {Object} callback	回调函数
 */
function xmlHttp(url,callback){
	if(url == ''){
		uexWindow.alert('参数错误','请求地址不能为空！','返回');
	}else{
		var platform = getLocalStorge('platform');
		url = url + "&pf="+platform;
		$.getJSON(url,callback);
	}
}

/**
 * 获取新闻列表
 * @param {Object} cid
 */
function  getNewsList(cid){
	uexWindow.toast("1","5","数据加载中…","0");
	localStorage.setItem("cid",cid);
	
	newsPage = 1;
	var url = apiHost + '&type=list&catid='+cid;
	xmlHttp(url,showList);
}

/**
 * 获取头部滚动新闻
 */
function getHeadNews(){
	var url = apiHost + '&type=header&catid=9';
	xmlHttp(url,showHeadNews);
}

/**
 * 显示头部滚动新闻
 * @param {Object} items
 */
function showHeadNews(items){
	for(var i in items){
		var item = items[i];
		
		$(".m_"+i.toString()).attr('id',item.id);
		//$(".m_"+i.toString()+" img").attr('src',item.thumb);
		
		//图片缓存
		imgCache('p'+i,item.thumb);
		$(".m_"+i.toString()+" p").html(b64.decode(item.title));
	}
}

/**
 * 图片缓存函数
 * @param {Object} rid
 * @param {Object} imgurl
 */
function imgCache(rid,imgurl){
	//获取图片后缀
	var extArr = new Array();
	extArr = imgurl.split('.');
	var ext = extArr[extArr.length-1];
	zy_imgcache(rid,imgurl,imgurl,changeImgDom,err,null,ext);
}

/**
 * 缓存图片失败的回调函数
 * @param {Object} rid
 * @param {Object} imgurl
 */
function err(rid,imgurl){
	$('#'+rid).attr('src',imgurl);
}

/**
 * 缓存图片成功的回调函数
 * @param {Object} rid
 * @param {Object} imgurl
 */
function changeImgDom(rid,imgurl){
	$('#'+rid).attr('src',imgurl);
}

/**
 * 根据新闻类型改变列表
 * @param {Object} cid
 */
function  changeNewsList(cid){
	uexWindow.toast("1","5","数据加载中…","0");
	uexWindow.evaluateScript("root","0","changeSelectedBg('"+cid+"')");
	
	if(cid == 8){
		$('.m-carousel').show();
	}else{
		$('.m-carousel').hide();
	}
	localStorage.setItem("cid",cid);

	newsPage = 1;
	var url = apiHost + '&type=list&catid='+cid;
	xmlHttp(url,showList);
}

/**
 * 显示新闻列表
 * @param {Object} items
 */
function showList(items){
	var outNewsObj = $("#newsList");
	outNewsObj.html('');
	
	for(var i in items){
		var item = items[i];
		
		if(item.thumb == ''){
            var thumb = 'css/images/default.jpg';
        }else{
            var thumb = item.thumb;
        }
		
		var tr = '';
		var trHeader = '<div ontouchstart="zy_touch(\'btn-newact\')" class="uc-n c-m4 ubb ub b-gra t-bla ub-ac lis newsItem" onclick="goNewsWindow('+item.id+')">';
		var trThumb = '<div class="leftThumb"><img src="'+thumb+'"></div>';
		var trTitle = '<div class="ub-f1 ub ub-ver"><div class="title">'+b64.decode(item.title)+'</div>';
		var trDesc = '<div class="desc">'+b64.decode(item.desc);
		var trDateline = '<span class="news_dateline">'+item.dateline+'</span></div>';
		var trFooter = '</div></div>';
		tr = trHeader+trThumb+trTitle+trDesc+trDateline+trFooter;
		outNewsObj.append(tr);
	}
	$("#newsList").show();
	$('#nextpage').show();
	uexWindow.closeToast();
}

/**
 * 显示下一页新闻
 * @param {Object} items
 */
function showMoreNews(items){
	var outNewsObj = $("#newsList");
	
	for(var i in items){
		var item = items[i];
		
		if(item.thumb == ''){
            var thumb = 'css/images/default.jpg';
        }else{
            var thumb = item.thumb;
        }

		var tr = '';
		var trHeader = '<div ontouchstart="zy_touch(\'btn-newact\')" class="uc-n c-m4 ubb ub b-gra t-bla ub-ac lis newsItem" onclick="goNewsWindow('+item.id+')">';
		var trThumb = '<div class="leftThumb"><img src="'+thumb+'"></div>';
		var trTitle = '<div class="ub-f1 ub ub-ver"><div class="title">'+b64.decode(item.title)+'</div>';
		var trDesc = '<div class="desc">'+b64.decode(item.desc);
		var trDateline = '<span class="news_dateline">'+item.dateline+'</span></div>';
		var trFooter = '</div></div>';
		tr = trHeader+trThumb+trTitle+trDesc+trDateline+trFooter;
		
		outNewsObj.append(tr);
	}
	$("#nextpage").text("查看下10条");
}

/**
 * 获取下一页新闻
 */
function getMoreNews(){
	var cid = localStorage.getItem('cid');
	if(cid == ''){
		cid = 8;
	}
	newsPage = newsPage + 1;
	var url = apiHost + '&type=list&catid='+cid+'&page='+newsPage;
	
	$("#nextpage").text("数据加载中……");
	xmlHttp(url,showMoreNews);
}

/**
 * 显示新闻窗口，跳转函数
 * @param {Object} aid
 */
function goNewsWindow(aid){
	if(aid == ''){
		uexWindow.alert("ERROR","未指定新闻ID,无法加载新闻内容！","返回");
	}else{
		localStorage.setItem("aid",aid);
		var newsUrl = "content.html";
		uexWindow.open("newsWindow",0,newsUrl,2,"","",0x0);
	}
}

/**
 * 显示新闻内容
 * @param {Object} newsid
 */
function showNews(newsid){
	uexWindow.toast("1","5","数据加载中…","0");
	if(newsid != 0){
		localStorage.setItem('aid',newsid)
	}
	var aid = localStorage.getItem("aid");
	
	if(aid == ''){
		uexWindow.alert("ERROR","未指定新闻ID,无法加载新闻内容！","返回");
		uexWindow.close(-1);
	}else{
		var url = apiHost + '&type=view&aid='+aid;
		xmlHttp(url,parseNews);
	}
}

/**
 * 新闻显示的回调函数
 * @param {Object} data
 */
function parseNews(data){
	
	$(".relatedNews").html('');
	
	var title = b64.decode(data.title);
	var dateline = data.dateline;
	var content = b64.decode(data.content);
	var comment = ' <a onclick="openComment()" style="text-decoration:none;color:#1582BD;font-weight:bold;" href="javascript:void(0)">网友热议('+data.total+')</a>';
	$(".nvtitle").html(title);
	$(".dateline").html(dateline+comment);
	$(".nvcontent").html(content);
	
	var upid = parseInt(data.upaid);
	var nextid = parseInt(data.nextaid);
	pageScroll();
	
	var addUpTHML = '';
	var addNextTHML = '';
	
	if(upid == 0){
		addUpTHML = "<p class='nextUpNews'>上一篇：没有了</p>";
	}else{
		addUpTHML = '<p class="nextUpNews" ontouchstart="changeBg(this)" ontouchend="resetBg(this)" onclick="showNews('+data.upaid+')">上一篇：'+b64.decode(data.uptitle)+'</p>';
	}
	//$(".relatedNews").append(addUpTHML);
	
	if(nextid == 0){
		addNextTHML = "<p class='nextUpNews'>下一篇：没有了</p>";
	}else{
		addNextHTML = '<p class="nextUpNews" ontouchstart="changeBg(this)" ontouchend="resetBg(this)" onclick="showNews('+data.nextaid+')">下一篇：'+b64.decode(data.nexttitle)+'</p>';
	}
	
	$(".relatedNews").append(addUpTHML + addNextHTML);
	uexWindow.closeToast();
}

/**
 * 头部滚动新闻的循环操作函数
 */
function cycle(){
	if(j > 3){
		j = 1;
	}
	$('.m-carousel').carousel('move', j);
	j++;
}

/**
 * 页面滚动到顶部,GoTop
 */
function pageScroll() {
    window.scrollBy(0,-50);
    scrolldelay = setTimeout('pageScroll()',1);
	var scrollTop = document.documentElement.scrollTop || window.pageYOffset;
    if(scrollTop==0){
         clearTimeout(scrolldelay);
    }
}

/**
 * 更改二级菜单背景
 * @param {Object} cid
 */
function changeSelectedBg(cid){
	var newID = cid.toString();
	$(".zhengchang").removeClass('anxia');
	$("#"+newID+"_cid").addClass("anxia");
}

/**
 * 关闭窗口
 */
function closeWindow(){
    var closeString = "uexWindow.close(-1);";
    uexWindow.evaluateScript('', '0', closeString);
}

/**
 * HTML5 localStorge.getItem 函数的封装
 * @param {Object} key
 */
function getLocalStorge(key){
	var item = localStorage.getItem(key);
	if(item=='' || item==null || typeof(item)=="undefined"){
		return false;
	}else{
		return item;
	}
}

/**
 * 打开评论窗口
 * 
 */
function openComment(){
	var aid = getLocalStorge('aid');
	if(aid.length == 0 ){
		uexWindow.alert('提示',"参数错误，新闻ID为空！","返回");
	}else{
		var commentUrl = "comment.html";
		uexWindow.open("commentWindow",0,commentUrl,2,"","",0x0);
	}
}

/**
 * 显示评论列表
 * 
 */
function showComments(){
	var aid = getLocalStorge('aid');
	if(aid.length == 0){
		uexWindow.alert('提示',"参数错误丢失，请返回重试！","返回");
		uexWindow.close(-1);
	}else{
		uexWindow.toast("1","5","数据加载中…","0");
		var url = apiHost + "&type=comment&aid="+aid;
		xmlHttp(url,showCommentResult);
	}
}

/**
 * 新闻评论列表回调函数
 * 
 * @param {Object} data
 */
function showCommentResult(items){
	var commentObj = $("#comment");
	commentObj.html('');
	j = 0;
	for(var i in items){
		j = j + 1;
		var item = items[i];
		var tr = '';
		var trHeader = '<div class="comment"><div><span class="commentTitle">'+b64.decode(item.author)+'</span>';
		var trDateline = '<span style="margin-left:1em;font-size:0.8em;color:#55a4dd;">'+item.dateline+'</span></div>';
		var trDesc = '<div class="commentContent">'+b64.decode(item.message);
		var trFooter = '</div></div>';
		tr = trHeader+trDateline+trDesc+trFooter;
		
		commentObj.append(tr);
	}
	var uid = getLocalStorge('uid');
	$('#total').text(j);
	uexWindow.closeToast();
}

/**
 * 
 * 新闻评论
 */
function postComment(){
	var uid = getLocalStorge('uid');
	if(!uid){
		var confirmButtom = new Array();
		confirmButtom[0] = '登录';
		confirmButtom[1] = '返回';
		uexWindow.confirm('提示：','请登录后再进行评论！',confirmButtom);
		return false;
	}else{
		uexWindow.toast("1","5","评论提交中……","0");
		var message = $('#message').val();
		localStorage.setItem('comment',message);
		var aid = getLocalStorge('aid');
		var uid = getLocalStorge('uid');
		var username = getLocalStorge('username');
		var password = getLocalStorge('pwd');
		var url = 'http://www.yeeyi.com/bbs/forum.php?jsoncallback=?&mod=mobile&ac=comment&aid='+aid+'&uid='+uid+'&username='+b64.encode(username)+'&pwd='+password+'&message='+b64.encode(message);
		xmlHttp(url,showResult);
	}
}

/**
 * 新闻评论回调函数
 * 
 * @param {Object} data
 */
function showResult(data){
	var code = data.info;
	if(code == '200'){
		uexWindow.closeToast();
		uexWindow.toast("0","5","新闻评论成功","2000");
		var message = getLocalStorge('comment');
		var commentObj = $("#comment");
		var tr = '';
		var trHeader = '<div class="comment"><div><span class="commentTitle">本人</span>';
		var trDateline = '<span style="margin-left:1em;font-size:0.8em;color:#55a4dd;">3秒钟前</span></div>';
		var trDesc = '<div class="commentContent">'+message;
		var trFooter = '</div></div>';
		tr = trHeader+trDesc+trFooter;
		commentObj.append(tr);
		$('#message').val('');
	}else if(code == '403'){
		uexWindow.alert('提示','评论失败，您的账号信息验证失败，请重新登录试试！','返回');
	}else if(code == '404'){
		uexWindow.alert('提示','评论失败，新闻ID丢失，请重试！','返回');
	}else{
		uexWindow.alert('提示','评论失败，未知错误，请联系管理员！','返回');
	}
}



/**
 * confirm控件监控函数
 * @param {Object} opId
 * @param {Object} dataType
 * @param {Object} data
 */
function confirmSuccess(opId,dataType,data){
	if(data == 0){
		uexWindow.open('login','0','login.html','1','100%','100%','0');
	}
}

function changeBg(obj){
	$(obj).addClass('btn_newact');
}

function resetBg(obj){
	$(obj).removeClass('btn_newact');
}


